<template>
  <div class="todo-footer">
    <label>
      <input type="checkbox" v-model="isAllSuccess" />
    </label>
    <span>
      <span>已完成{{successNum}}</span>/{{todoList.length}}全部
    </span>
    <button class="btn btn-danger" @click="deleteSuccess">清除已完成任务</button>

    <button class="btn btn-danger"  id="login" @click="gotoLogin">回到登录页面</button>
  </div>
</template>

<script>
import {mapMutations, mapState} from "vuex"
export default {
  name: "Footer",
  computed:{
    //拿到数组
    ...mapState(["todoList"]),

    //已完成数量
    successNum(){
      return this.todoList.reduce((p , c) => c.done ? p + 1 : p , 0) 
    },

    //全选数量
    isAllSuccess: {
      set(value){
        //提交全选函数
        this.allChecked(value)
      },

      get(){
        return this.todoList.every((item) => item.done) && this.todoList.length !== 0;
      }
    }
  },

  methods:{
    //回到登录页
    gotoLogin(){
      this.$router.push("/login")
    },

    //映射全选框函数
    ...mapMutations(["allChecked" , "deleteSuccess"])
  }

};
</script>

<style scoped>
/*footer*/
.todo-footer {
  height: 40px;
  line-height: 40px;
  padding-left: 6px;
  margin-top: 5px;
}

.todo-footer label {
  display: inline-block;
  margin-right: 20px;
  cursor: pointer;
}

.todo-footer label input {
  position: relative;
  top: -1px;
  vertical-align: middle;
  margin-right: 5px;
}

.todo-footer button {
  float: right;
  margin-top: 5px;
}

#login{
  margin-right: 120px;
  background-color: deepskyblue;
}
</style>